package xyz.fefine;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by feng_ on 2016/9/16.
 * 使用java本地方法进行执行
 */
public class NativeFunction {
    protected Method method;
    protected String name;
    protected int numParams;

    /**
     *
     * @param method 方法
     * @param name 方法名
     */
    public NativeFunction(Method method, String name) {
        this.method = method;
        this.name = name;
        numParams = method.getParameterCount();
    }

    @Override
    public String toString() {
        return "<Native Fun " + hashCode() + ">";
    }

    public int getNumParams() {
        return numParams;
    }

    public Object invoke(Object[] args, ASTree asTree) {
        try {
            //静态方法,对象为null
            return method.invoke(null, args);
        } catch (Exception e) {
            throw new StoneException("bad native function call:" + name + asTree);
        }
    }

}
